package t327.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import t327.mapper.SubmissionPaLnMapper;
import t327.pojo.*;
import t327.pojo.font.LuiTableEntity;
import t327.pojo.until.Result;
import t327.pojo.views.ContractOrderClientView;
import t327.pojo.views.OrderColorSizeClientView;

import t327.pojo.views.SubmissionPaLnView;
import t327.service.iter.ITailorService;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("tailor")
public class TailorController {
    @Autowired
    private ITailorService tailorService;
    @RequestMapping("addTakeAwaySubmit")
    public Result addcuttingDepartment(@RequestBody TakeAwaySubmit takeAwaySubmit){
        Long orderId = takeAwaySubmit.getOrderId();
        Long productId = takeAwaySubmit.getProductId();
        Date takeAwaySubmitTime = takeAwaySubmit.getTakeAwaySubmitTime();
        Long styleId = takeAwaySubmit.getStyleId();
        Long takeAwayCount = takeAwaySubmit.getTakeAwayCount();
        Long materialsId = takeAwaySubmit.getMaterialsId();
        int i = tailorService.addTakeAwaySubmit(takeAwaySubmit);
        if (orderId<=0L || productId<=0L || styleId<=0L ||takeAwayCount<=0 ||materialsId<=0){
            return new Result(221,"请填写全部，不能有空哦",false,null);
        }else {
            if (!isNumeric(takeAwayCount.toString())){
                return new Result(222,"请填写数字",false,null);
            }
            if (isValidDate(takeAwaySubmitTime.toString(),"yyyy-MM-dd")){
                return new Result(204,"请选择正确的时间格式",false,null);
            }
            if (i>0){
                return new Result(200,"申请成功",false,null);
            }
            return new Result(230,"申请失败",false,null);
        }
    }
    //验证是单价和总价是否是数字
    public boolean isNumeric(String input) {
        try {
            Long.parseLong(input);  // 尝试将输入解析为整数
            // 或
            // Double.parseDouble(input);  // 尝试将输入解析为浮点数
            return true;  // 解析成功，输入为数字
        } catch (NumberFormatException e) {
            return false;  // 解析失败，输入不是数字
        }
    }
    //验证时间是否正确
    public  boolean isValidDate(String dateString, String pattern) {
        try {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
            LocalDate.parse(dateString, formatter);
            return true;  // 日期格式正确
        } catch (DateTimeParseException e) {
            return false;  // 日期格式错误
        }
    }
    @RequestMapping("queryMaterials")
    public List<Materials> queryMaterials(){
        return tailorService.queryMaterials();
    }
    @RequestMapping("getProductDetail")
    public List<ProductDetail> getProductDetail(){
        return tailorService.getProductDetail();
    }
    @RequestMapping("queryStyleNumber")
    public List<StyleNumber> queryStyleNumber(){
        return tailorService.queryStyleNumber();
    }
    //查询已生产的计划
    @RequestMapping("querySubPlan")
    public LuiTableEntity querySubPlan(@RequestParam(value = "page") Integer pageNum, @RequestParam(value = "limit")Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        LuiTableEntity luiTableEntity = new LuiTableEntity<OrderColorSizeClientView>();
        PageInfo<SubmissionPaLnView> submissionPaLnViewPageInfo = new PageInfo<>(tailorService.queryAll());
        List<SubmissionPaLnView> submissionPaLnViews = submissionPaLnViewPageInfo.getList();
        long total = submissionPaLnViewPageInfo.getTotal();
        if(submissionPaLnViews!=null&&submissionPaLnViews.size()>0){
            luiTableEntity.setCode("0");
            luiTableEntity.setMsg("查询成功");
            luiTableEntity.setCount(total);
            luiTableEntity.setData(submissionPaLnViews);
        }else{
            luiTableEntity.setCode("202");
            luiTableEntity.setMsg("查询失败");
        }
        return   luiTableEntity;
    }
    @RequestMapping("delsubmission")
    public Result delsubmission(Long id){
        int i = tailorService.delSubmission(id);
        if (i>0){
            return Result.success();
        }
        return  new Result(201,"删除失败",false,null);
    }
    @RequestMapping("upsubmission")
    public Result upsubmission(Long producedCount,Long id){
        int i = tailorService.upSubmission(producedCount,id);
        if (i>0){
            return Result.success();
        }
        return  new Result(201,"修改产量失败",false,null);
    }
    //查询裁剪的实际生产
    @RequestMapping("renderThePageByCuttingTheActualProductionPlan")
    public List<SubmissionPaLnView> renderThePageByCuttingTheActualProductionPlan(){
      return   tailorService.queryAll();
    }
    //查询裁剪的实际生产
    @RequestMapping("checkSewingProductionQuantity")
    public List<Submission> checkSewingProductionQuantity(){
        return   tailorService.checkSewingProductionQuantity();
    }
}